package com.cq.sphtech.flow;


import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.cq.sphtech.R;
import com.cq.sphtech.flow.adapter.HomeAdapter;
import com.cq.sphtech.model.FlowInfo;
import com.cq.sphtech.util.DataUtils;

import java.util.ArrayList;
import java.util.List;


public class FlowInfoFragment extends Fragment implements FlowInfoContract.View {
    private RecyclerView mRecyclerView;
    private HomeAdapter mHomeAdapter;

    private Dialog mDialog;
    private List<List<FlowInfo.RecordsBean>> mList = new ArrayList<>();
    private FlowInfoContract.Presenter mPresenter;

    public static FlowInfoFragment newInstance() {
        return new FlowInfoFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_ipinfo, container, false);
        mRecyclerView = root.findViewById(R.id.id_recycler_view);
        return root;
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mDialog = new ProgressDialog(getActivity());
        mDialog.setTitle("获取数据中");
        mPresenter.getIdInfo("a807b7ab-6cad-4aa6-87d0-e283a7353a0f");
        initView();
    }

    public void initView() {
        // 设置布局管理器
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mHomeAdapter = new HomeAdapter(getContext(), mList);
        mHomeAdapter.setOnItemClickListener(new HomeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(getContext(), "该年存在数据量下降", Toast.LENGTH_LONG).show();
            }
        });
        mRecyclerView.setAdapter(mHomeAdapter);

    }

    @Override
    public void onPause() {
        super.onPause();
        mPresenter.unsubscribe();
    }

    @Override
    public void setPresenter(FlowInfoContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void setInfo(FlowInfo flowInfo) {
        if (DataUtils.DealInfo(flowInfo) != null) {
            mList.clear();
            mList.addAll(DataUtils.DealInfo(flowInfo));
            for (List<FlowInfo.RecordsBean> item : mList) {
                Log.d("MVPC", item.toString());
            }
            mHomeAdapter.notifyDataSetChanged();

        } else {
            Log.d("MVP_C", "数据为空！");
        }

    }

    @Override
    public void showLoading() {
        mDialog.show();
    }

    @Override
    public void hideLoading() {
        if (mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    @Override
    public void showError() {
        Toast.makeText(getActivity().getApplicationContext(), "网络出错", Toast.LENGTH_SHORT).show();
        Log.d("MVP_C", "网络出错");
    }

    @Override
    public boolean isActive() {
        return isAdded();
    }
}